package com.dajiba.demo;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Looper;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;

import static android.animation.ValueAnimator.REVERSE;

public class CustomDrawBeatLine extends BaseCustomView implements ValueAnimator.AnimatorUpdateListener {

    private int mLineY = 0;
    private ValueAnimator mValueAnimator;
    private int width = 0;
    private int height = 0;
    private long duration = 2000;
    private long mLineSum = 4;
    private int mLineInterval = 50;
    private int mLineWidth = 10;
    private Paint mPaint;

    public CustomDrawBeatLine(Context context) {
        super(context);
        init();
    }

    public CustomDrawBeatLine(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public CustomDrawBeatLine(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        setBackgroundColor(Color.BLACK);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        if (mValueAnimator == null) {
            initAnim();
        }
        setMeasuredDimension(width, height);
    }

    private void initAnim() {
        mValueAnimator = ValueAnimator.ofInt(0, height);
        mValueAnimator.setDuration(duration);
        mValueAnimator.addUpdateListener(this);
        mValueAnimator.setRepeatCount(-1);
        mValueAnimator.setRepeatMode(REVERSE);
        start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStrokeWidth(mLineWidth);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        int offsetLineWidth = mLineWidth / 2;
        int lineInterval = mLineInterval;
        for (int i = 0; i < mLineSum; i++) {
            if (i == 0) {
                canvas.drawLine(offsetLineWidth, mLineY + offsetLineWidth, offsetLineWidth, height - offsetLineWidth, mPaint);
                lineInterval = mLineInterval;
            } else {
                if (i % 2 == 1) {
                    canvas.drawLine(lineInterval, height - mLineY + offsetLineWidth, lineInterval, height - offsetLineWidth, mPaint);
                } else {
                    canvas.drawLine(lineInterval, mLineY + offsetLineWidth, lineInterval, height - offsetLineWidth, mPaint);
                }
                lineInterval = lineInterval + mLineInterval;
            }
        }
    }

    private void start() {
        mValueAnimator.start();
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        mLineY = (int) animation.getAnimatedValue();
        LogUtils.d("onAnimationUpdate", mLineY, height);
        invalidate();
    }
}
